//
//  ProbegVC.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/19/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit

final class ProbegViewController: UIViewController, Alertable {
    
    // MARK: - Outlets
    
     private lazy var submitBtn: UIButton = {
        let btn = UIButton()
        btn.clipsToBounds = true
        btn.layer.cornerRadius = 15
        btn.setTitle("Продолжить", for: .normal)
        btn.backgroundColor = .blueColor
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel!.font = UIFont.systemFont(ofSize: 17, weight: .medium)
        return btn
    }()

    private lazy var probegModalView: ModalView = {
        let modalView = ModalView()
        modalView.delegate = self
        return modalView
    }()
    
    
    private lazy var probegField: ModalElement = {
        let element = ModalElement()
        element.updateUI(value: nil, placeholder: "Пробег, км", withTF: true, keyboardType: .numberPad)
        return element
    }()
    
    private lazy var bityiToggle = ToggleView()
    
    // MARK: - Props
    
    public var carFilter: CarFilter!
    public var mode: MarkaModelMode!
    public var resultsVC: ResultsVCDelegate!
    
    // MARK: - UI Actions
    
    @objc private func goBack (sender: UIBarButtonItem) {
        self.navigationController?.popViewController(animated: true)
    }

    @objc private func closeVC (sender: UIBarButtonItem) {
        self.navigationController?.dismiss(animated: true, completion: nil)
    }
    
    @objc private func onSubmit (sender: UIButton) {
        if self.probegField.getTextFieldValue().isEmpty {
            self.showAlert("Упс...", "Введите значение пробега")
        } else {
            carFilter.probeg = Int(self.probegField.getTextFieldValue()) ?? nil
            resultsVC.goBack(data: carFilter, type: mode)
            goToRoot()
        }
    }
    
    @objc private func screenTap (sender: UITapGestureRecognizer) {
        self.probegField.closeTextField()
    }
    
    
    // MARK: - Life-Cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUpUI()
        self.setUpNavBar()
    }

}

// MARK: - Methods


extension ProbegViewController {
    
}

extension ProbegViewController: ModalViewDelegate {
    func onCancel() {
        //
    }
    
    func onSubmit(for type: ModalView.ModalType, data: Any) {
        print(data)
    }
    func showError(_ title: String, _ message: String) {
        self.showAlert(title, message)
    }
}


extension ProbegViewController: ModalElementProtocol {
    func selectElement(modal: ModalView.ModalType?, customID: Int?) {
        self.probegModalView.updateModalType(type: .probeg)
        self.view.addSubview(probegModalView)
        probegModalView.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(view.safeAreaLayoutGuide)
            make.height.equalTo(345)
        }
    }
    
    func didClear(modal: ModalView.ModalType) {}
}

// MAKR: - UI

extension ProbegViewController {
    
    private func setUpUI () {
        view.backgroundColor = .white
        self.view.addSubview(probegField)
        self.view.addSubview(bityiToggle)
        self.view.addSubview(submitBtn)
        self.addConstraints()
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(screenTap(sender:)))
        self.view.addGestureRecognizer(tapGesture)
        
        submitBtn.addTarget(self, action: #selector(onSubmit(sender:)), for: .touchUpInside)
        
        bityiToggle.makeToggleView(name: "Битый или не на ходу", isOn: false)
        self.addCloseIcon()
    }
    
    private func addConstraints () {
        submitBtn.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.bottom.equalTo(view.safeAreaLayoutGuide).offset(-16)
            make.height.equalTo(44)
        }
        probegField.snp.makeConstraints { (make) in
            make.top.equalTo(view.safeAreaLayoutGuide).offset(30)
            make.height.equalTo(48)
            make.leading.trailing.equalToSuperview().inset(16)
        }
        bityiToggle.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(64)
            make.top.equalTo(probegField.snp.bottom).offset(24)
        }
    }
    
    private func setUpNavBar () {
        let backIcon = UIImage(named: "cancel")
        let backButton = UIBarButtonItem(image: backIcon, landscapeImagePhone: backIcon, style: .plain, target: self, action: #selector(goBack(sender:)))
        backButton.tintColor = .greyColor
        
        self.navigationItem.simpleNavBar(title: "Пробег", backButton: backButton)
        navigationItem.largeTitleDisplayMode = .always
        navigationController?.navigationBar.largeTitleTextAttributes = [
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 24, weight: .bold)
        ]
        navigationItem.hidesSearchBarWhenScrolling = false
        self.navigationController?.navigationItem.largeTitleDisplayMode = .always
    }
    
    private func addCloseIcon () {
        let closeBarBnt = UIBarButtonItem(image: UIImage(named: "dismiss"), style: .plain, target: self, action: #selector(closeVC))
        closeBarBnt.tintColor = .greyColor
        self.navigationItem.rightBarButtonItem = closeBarBnt
    }
    
    private func goToRoot () {
        self.navigationController?.dismiss(animated: true, completion: nil)
    }
}





